import streamlit as st
import re
import data.data as dd
import time
import base64
import model.model as mm
st.set_page_config(
    page_title="问答助手修改密码页面",
    page_icon="😊"
)
mm.page_config()
st.html("""
        <style>
        .st-emotion-cache-1gv3huu button{
            width:100% !important;
        }
        #root > div:nth-child(1) > div.withScreencast > div > div > div > section.st-emotion-cache-1gv3huu.eczjsme18 > div.st-emotion-cache-6qob1r.eczjsme11 > div.st-emotion-cache-1gwvy71.eczjsme12 > div > div > div > div > div:nth-child(1) > div > div > h1{
        text-align:center;
        }
    </style>
    """)
st.title('问答助手修改密码页面')
username = st.text_input("请输入账号")
originpassword = st.text_input("请输入原密码",type='password')
password = st.text_input("请输入修改密码",type='password')
repass = st.text_input("请再次确认修改密码",type='password')
resetFlag = st.button("确认修改")
# loginFlag = st.button("取消修改，点击登录")

def reset(originpassword,username,password,repass):
    if username and password and repass:
        if re.match('^(13|15|17|18|19)[0-9]{9}$', username):
            result = dd.query_user_password(username)
            if result["password"] == originpassword:
                if password == repass and len(password) >= 8:
                    if dd.query_user(username):
                        if dd.update_message(username,password):
                            st.success("修改成功")
                            time.sleep(1)
                            st.switch_page("login.py")
                        else:
                            st.error('修改失败，请联系管理员')
                    else:
                        st.error("该用户不存在，请先注册")
                else:
                    st.error("两次密码必须一致或者密码长度不足8位")
            else:
                st.error("原密码错误，请重新输入")
        else:
            st.error("请输入正确的手机号")
    else:
        st.error("请完善信息")

if resetFlag:
    reset(originpassword,username,password,repass)

# if loginFlag:
#     st.switch_page("login.py")

def main_bg(main_bg):
    main_bg_ext = "png"
    st.markdown(
        f"""
         <style>
         .stApp {{
             background: url(data:image/{main_bg_ext};base64,{base64.b64encode(open(main_bg, "rb").read()).decode()});
             background-size: cover
         }}
         </style>
         """,
        unsafe_allow_html=True
    )

# 调用
main_bg('images/login6.png')